package com.sniff.atp.service.purchase;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.sniff.atp.dto.SystemLoginDTO;
import com.sniff.atp.enums.api.PurchaseAPI;
import com.sniff.atp.utils.*;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @BelongsProject: sniff-atp
 * @BelongsPackage: com.sniff.atp.service.purchase
 * @Author: zxy
 * @CreateTime: 2022-11-01 17:44
 * @Description: OEM采购流程
 * @Version: 1.0
 */
@Service
public class OEMPurchaseService {
    private SystemLoginDTO systemLoginDTO;

    /**
     * 平台端；设置登录token
     * @param systemLoginDTO
     */
    public void setPurchaseToken(SystemLoginDTO systemLoginDTO){
        this.systemLoginDTO = systemLoginDTO;
    }

    /**
     * 平台端：oem采购单查询
     */
    public String pagePurchase(String oemPurchaseNo){
        JSONObject param = JsonUtil.readJson("purchase/oemPagePurchase");
        param.replace("oemPurchaseNo", oemPurchaseNo);

        return new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO)).doPostJson(PurchaseAPI.PAGE_PURCHASE.getApi(), param);
    }

    /**
     * 平台端：oem采购查询支付账号
     * @param systemSourceType: 平台：1 2C, 2 2B
     */
    public JSONObject accountQuery(String systemSourceType){
        JSONObject param = JsonUtil.readJson("purchase/accountQuery");
        param.replace("systemSourceType", systemSourceType);

        String result = new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO)).doPostJson(PurchaseAPI.ACCOUNT_QUERY.getApi(), param);
        return JSONObject.parseObject(result,JSONObject.class);
    }

    /**
     * @Description: 平台端：oem采购单编辑商品信息
     * @param: oemPurchaseItemId：oem采购单商品id
     * @param: purchaseNum：采购数量
     * @param: sellUnitPrice：销售单价
     * @param: purchaseUnitPrice：采购单价
     * @param: productImg：商品sku图片
     * @Return: com.alibaba.fastjson2.JSONObject
     * @Author: zxy
     * @Date: 2022/11/2 10:58
     * @Version: 1.0
     */
    public JSONObject editItemInfo(String oemPurchaseItemId, Integer purchaseNum
            , Float sellUnitPrice, Float purchaseUnitPrice, String productImg){
        JSONObject param = JsonUtil.readJson("purchase/editItemInfo");

        param.replace("oemPurchaseItemId",oemPurchaseItemId);
        param.replace("purchaseAmount",(purchaseNum * purchaseUnitPrice));
        param.replace("purchaseNum",purchaseNum);
        param.replace("sellUnitPrice",sellUnitPrice);
        param.replace("purchaseUnitPrice",purchaseUnitPrice);
        param.replace("productImg",productImg);

        String result = new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO)).doPostJson(PurchaseAPI.EDIT_ITEM_INFO.getApi(), param);

        return JSONObject.parseObject(result, JSONObject.class);
    }

    /**
     * @Description: 平台端：oem采购单编辑采购单信息
     * @param: oemPurchaseId：oem采购单id
     * @param: payAmount：采购总价
     * @Return: com.alibaba.fastjson2.JSONObject
     * @Author: zwg
     * @Date: 2023/9/7 11:18
     * @Version: 1.0
     */
    public JSONObject editOemPurchaseOrder(String oemPurchaseId, Float payAmount){
        JSONObject param = JsonUtil.readJson("purchase/editOemPurchaseOrder");

        String tbAccount = "OEMtbAccount" + new Date().getTime();
        String tbOrderNo = "OEMtbOrderNo" + new Date().getTime();

        param.replace("oemPurchaseId",oemPurchaseId);
        param.replace("tbAccount",tbAccount);
        param.replace("tbOrderNo",tbOrderNo);

        JSONObject paymentList = param.getJSONArray("paymentList").getJSONObject(0);
        String approvalNo = "OEMSP" + new Date().getTime();
        paymentList.replace("approvalNo",approvalNo);
        paymentList.replace("payTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date().getTime()));
        paymentList.replace("payAmount",payAmount);

        String result = new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO)).doPostJson(PurchaseAPI.EDIT_ITEM_PURCHASE_ORDER.getApi(), param);

        return JSONObject.parseObject(result, JSONObject.class);
    }

    /**
     * 平台端：oem采购填写物流单号
     */
    public JSONObject addPurchaseExpressNo(String purchaseId){
        JSONObject param = new JSONObject();
        param.put("purchaseId", purchaseId);

        JSONObject expressNoDo = new JSONObject();
        String logisticsNumber = "OEMWL" + new Date().getTime();
        expressNoDo.put("logisticsNumber",logisticsNumber);
        expressNoDo.put("logisticsTag","");
        expressNoDo.put("purchaseEstimateStockTime","2035-10-01");
        JSONArray expressNo = new JSONArray();
        expressNo.add(expressNoDo);

        param.put("expressNo", expressNo);

        ToolsUtil.stringBuild("oemLogisticsNumber", logisticsNumber);

        String result = new HttpClientUtil(Headers.setSystemHeader(systemLoginDTO)).doPostJson(PurchaseAPI.ADD_PURCHASE_EXPRESS_NO.getApi(), param);
        return JSONObject.parseObject(result, JSONObject.class);
    }

    /**
     * 平台端：oem采购流程
     */
    public void oemPurchaseProcess(SystemLoginDTO systemLoginDTO, String oemPurchaseNo){

        setPurchaseToken(systemLoginDTO);

        String pagePurchase = pagePurchase(oemPurchaseNo);
        LogUtil.warn("【oem采购】查询采购单号：" + oemPurchaseNo);
        String oemPurchaseId = JsonUtil.extractString(pagePurchase, "data.records[0].oemPurchaseId");
        JSONArray orderItemList = JsonUtil.extractJSONArray(pagePurchase, "data.records[0].orderItemList");

        Float payAmount = Float.valueOf(0);
        for (Object orderItem : orderItemList) {
            String oemPurchaseItemId = ((JSONObject) orderItem).getString("oemPurchaseItemId");
            Integer sellQuantity = ((JSONObject) orderItem).getInteger("sellQuantity");
            Float sellUnitPrice = ((JSONObject) orderItem).getFloat("sellUnitPrice");
            Float purchaseUnitPrice = ((JSONObject) orderItem).getFloat("purchaseUnitPrice");
            String productImg = ((JSONObject) orderItem).getString("productImg");
            payAmount += sellQuantity * purchaseUnitPrice;

            editItemInfo(oemPurchaseItemId, sellQuantity, sellUnitPrice, purchaseUnitPrice, productImg);
            LogUtil.warn("【oem采购】采购单商品信息编辑：采购数量 = 销售数量");
        }

        editOemPurchaseOrder(oemPurchaseId, payAmount);
        LogUtil.warn("【oem采购】采购单信息编辑：国内运费国定：20，客户运费固定：5");

        addPurchaseExpressNo(oemPurchaseId);
        LogUtil.warn("【oem采购】添加物流单号，采购完成！！！！");

    }
}
